﻿using System;
using System.Net;
using System.Collections.Generic;

namespace MyMailWp8.Protocol
{
    internal class CMPopList : CMPopBase
    {
        private int listState = 0;//

        private String stringDatabuf;
        private List<string> uidList = new List<string>();
        private List<int> mailSizeList = new List<int>();

        public CMPopList(AsyncSocketClient sockClient)
            : base(sockClient)
        { }

        public bool listMail()
        {
            stringDatabuf = null;
            listState = 0;

            SendData("LIST\r\n");
            return true;
        }

        public bool uidlMail()
        {
            stringDatabuf = null;
            listState = 10;

            SendData("UIDL\r\n");
            return true;
        }

        public List<string> getMailUidlList()
        {
            return uidList;
        }

        private bool readMailSizeList()
        {
            while (true)
            {
                int nTrimpos = stringDatabuf.IndexOf("\r\n");
                if (nTrimpos == -1)
                    break;

                String data2Parse = stringDatabuf.Substring(0, nTrimpos);
                {
                    int ntemp = data2Parse.IndexOf(" ");
                    if (ntemp == -1)
                        break;

                    data2Parse = data2Parse.Substring(ntemp + 1);
                    mailSizeList.Add(Convert.ToInt32(data2Parse));
                }

                nTrimpos += 2;//pass away "/r/n"
                stringDatabuf = stringDatabuf.Substring(nTrimpos);
            }

            return true;
        }

        private bool readMailUidlList()
        {
            while (true)
            {
                int nTrimpos = stringDatabuf.IndexOf("\r\n");
                if (nTrimpos == -1)
                    break;

                String data2Parse = stringDatabuf.Substring(0, nTrimpos);
                {
                    int ntemp = data2Parse.IndexOf(" ");
                    if (ntemp == -1)
                        break;

                    data2Parse = data2Parse.Substring(ntemp + 1);
                    uidList.Add(data2Parse);
                }

                nTrimpos += 2;//pass away "/r/n"
                stringDatabuf = stringDatabuf.Substring(nTrimpos);
            }

            return true;
        }

        public override int ParseData(String inString)
        {
            if (inString == null)
                return 0;

            if (stringDatabuf == null)
                stringDatabuf = String.Copy(inString);
            else
                stringDatabuf += inString;

            if (stringDatabuf.EndsWith(".\r\n"))
            {
                //get all list data
                if (stringDatabuf.ToUpper().StartsWith("+OK") == true)
                {
                    int nTrimpos = stringDatabuf.IndexOf("\r\n");
                    if (nTrimpos == -1)
                        return 0;

                    nTrimpos += 2;//pass away "/r/n"
                    stringDatabuf = stringDatabuf.Substring(nTrimpos);
                    if (listState == 0)
                    {
                        readMailSizeList();
                        //
                        uidlMail();
                    }
                    else
                    {
                        readMailUidlList();
                        int nState = (int)(CMPopState.CMPOP_ListEnd);
                        ChangeCMPopSmtpState(nState);
                    }
                }
                else
                {
                    //DefaultLogger.Log.LogDebug("CMPopList:parseData " + "not received +ok\r\n");
                    //PostServerRetError((int)SockMessageType.ListFail);
                }
            }
            else
            {
                //read more data
                ReceiveData();
            }

            return 0;
        }
    }
}
